package org.mtmi.ui.controls.edits;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.mtmi.ui.M;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.Label;
import org.mtmi.ui.controls.Spinner;
import org.mtmi.ui.controls.listeners.ValueChangedListener;
import org.mtmi.ui.controls.listeners.ValueEvent;
import org.mtmi.ui.controls.popups.AbstractButtonPopup;
import org.mtmi.ui.layouts.GridData;
import org.mtmi.ui.layouts.GridLayout;
import org.mtmi.ui.layouts.LayoutData;
import org.mtmi.ui.scenes.Scene;

public class MinMaxPopup extends AbstractButtonPopup
  implements ValueChangedListener
{
	//min/max completly choosen
	private static final int MIN=-1000;
	private static final int MAX=+1000;
	
	private Label lb_min, lb_max;
	private Spinner sp_min , sp_max;
	private Control controls[];
	private int vmin, vmax;
	
	public MinMaxPopup( Scene scene, String _title, int flags ) {
		super(scene, _title, flags);
		vmin= MIN;
		vmax= MAX;
	}
	
	public void setRange( int min, int max ) {
		vmin = min;
		vmax = max;
		lb_min.setText( Integer.toString(vmin) );
		lb_max.setText( Integer.toString(vmax) );
		sp_min.setValue( vmin );
		sp_max.setValue( vmax );
	}

	public int getMin() { return vmin; }
	public int getMax() { return vmax; }
	
	@Override
	public Rectangle createContents( Scene _scene )
	{
		int fe = getFlags() & M.SCENE_EDITOR_CONTROL;
		
		//use this text to compute label bounds
		String lt = getLongestText();
		controls = new Control[] {
		   new Label( _scene, "Minimum:", fe ),
		   sp_min = new Spinner( _scene, vmin, MIN,MAX, fe ),
		   lb_min = new Label( _scene, lt, fe | M. RIGHT | M.VCENTER ),
		   new Label( _scene, "Maximum:", fe ),
		   sp_max = new Spinner( _scene, vmax, MIN,MAX, fe ),
		   lb_max = new Label( _scene, lt, fe | M. RIGHT | M.VCENTER )
		};
		
		sp_min.addValueChangedListener( this );
		sp_max.addValueChangedListener( this );
		
		lb_min.setData( M.D_LAYOUT_DATA, new GridData( M.RIGHT, false, M.FILL_VERTICAL, false ) );
		lb_max.setData( M.D_LAYOUT_DATA, new GridData( M.RIGHT, false, M.FILL_VERTICAL, false ) );
		
		GridLayout gl = new GridLayout( 3, false );
		gl.setSpacings( 20 );
		LayoutData ld = new LayoutData( _scene );
		Point sz = gl.computeSize( ld, controls );
		Point s = _scene.getSize();
		int x = (s.x-sz.x)/2;
		int y = (s.y-sz.y)/2;
		ld.lx = x; ld.ly = y;
		ld.lw = sz.x; ld.lh = sz.y;
		gl.layout( ld, controls );
		
		lb_min.setText(""); //$NON-NLS-1$
		lb_max.setText(""); //$NON-NLS-1$
		
		return new Rectangle( x, y, sz.x, sz.y );
	}
	
	private String getLongestText()
	{
		int abs = Math.max( Math.abs(MAX), Math.abs(MIN) );
		StringBuilder sb = new StringBuilder();
		if( MIN<0 || MAX < 0 ) {
			sb.append( '-' );
		}
		while( abs > 0 ) {
			sb.append('8');
			abs /= 10;
		}
		return sb.toString();
	}

	@Override
	public void dispose() {
		for( int i=0; i<controls.length; i++ ) {
			controls[i].dispose();
		}
		super.dispose();
	}

	@Override
	public void valueChanged(ValueEvent event) {
		if( event.control == sp_min ) {
			int nvn = event.newValue;
			if( nvn >= vmax ) {
				vmax = nvn;
				if( vmax >= MAX ) {
					vmax = MAX;
					nvn = MAX;
				}
			}
			if( nvn!=vmin ) {
				setRange( nvn, vmax );
			}
		}
		else { //sl_max dragged
			int nvx = event.newValue;
			if( nvx <= vmin ) {
				vmin = nvx;
				if( vmin <= MIN ) {
					vmin = MIN;
					nvx = MIN;
				}
			}
			if( nvx!=vmax ) {
				setRange( vmin, nvx );
			}
		}
	}
}
